package db;

import Info.SystemEnum;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Car;
import pojo.Collect;
import pojo.User;

import java.util.List;
import java.util.Map;

public class ManageCollectDB {
    private Map<User,Collect> collectMap;
    private Log log;


    public ManageCollectDB(Map<User, Collect> collectMap) {
        this.collectMap = collectMap;
        log = LogFactory.getLog(getClass());
    }

    public ManageCollectDB(Log log) {
        collectMap = DataBase.getInstance().getCollectsMap();
        this.log = log;
    }

    //查找是否存在该键值对
    public boolean find(User user){
        if(collectMap.containsKey(user)){
            return true;
        }else return false;
    }
    //查找某键中是否有某个值
    public int find(User user,Car car){
       if(find(user)){
           int index = collectMap.get(user).listFind(car);
           if(index!=-1)
               return index;
       }
       return -1;
    }
    //添加键值对
    public boolean add(User user){
        if(user!=null){
            if(!find(user)){
                collectMap.put(user, new Collect(user));
                log.info(getClass()+"\tadd-new-K-V\t"+ SystemEnum.SUCCESS.toString());
                return true;
            }else{
                log.info(SystemEnum.ERROR_REPEAT.toString());
                return false;
            }
        }
        log.info(SystemEnum.ERROR_MATCH.toString());
        return false;
    }
    //为键值对  添加值
    public boolean add(User user,Car car){
        if(find(user)){
                Collect collect = collectMap.get(user);
                collect.listAdd(car);
                log.info(collect.toString());
                log.info(getClass()+"\tadd-new-V\t"+ SystemEnum.SUCCESS.toString());
                return true;
            }else{
               log.info(getClass()+"\tadd-new-V\t"+ SystemEnum.ERROR_INPUT.toString());
                return false;
            }
    }

    //删除-键值对
    public boolean delete(User user){
        if(find(user)){
            collectMap.remove(user);
            log.info(getClass()+"\tdelete-K-V\t"+ SystemEnum.SUCCESS.toString());
            return true;
        }else{
            log.info(getClass()+"\tdelete-K-V\t"+ SystemEnum.ERROR_MATCH.toString());
            return false;
        }
    }
    //删除-值List中的一个值
    public boolean delete(User user,Car car){
        if(find(user,car)!=-1){
            return collectMap.get(user).listDelete(car);
        }
        return false;
    }
    public List<Car> getAUserCollect(User user){
        if(find(user)){
            return collectMap.get(user).getList();
        }
        return null;
    }

    public void printAll(){
        System.out.println(collectMap);
    }

    public void printAUserCollect(User user){
        Collect collect = collectMap.get(user);
        if(collect!=null){
            log.info("user : "+user.toString());
            log.info(collect.toString());
        }else System.out.println("该用户不存在收藏");
    }
}